package com.jk.controller.login;

import com.jk.constants.LoginConstants;
import com.jk.pojo.PayUser;
import com.jk.service.login.LoginService;
import com.jk.upload.OSSUploadUtil;
import com.jk.util.EmailInfo;
import com.jk.util.ShortMsg;
import com.jk.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * &lt;pre&gt;项目名称：jk-pay
 * 类名称：LoginController
 * 类描述：用于实现用户的登录、注册
 * 创建人：李金春
 * 创建时间：2017-12-20 16:05:59
 * 修改人：李金春
 * 修改时间：2017-12-20 16:06:04
 * 修改备注：
 *
 * @version ${enclosing_type}${tags}&lt;/pre&gt;
 */
@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**上传头像*/
    @PostMapping("upLoadImg")
    String upLoadImg(MultipartFile file){
        //获取到文件的名字
        String fileName = file.getOriginalFilename();
        //获取到文件的后缀名
        String prifix = fileName.substring(fileName.lastIndexOf(".")+1);
        File f = null;
        try {
            //将文件转为file类型
            f=File.createTempFile("tmp",null);
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //使用工具类将上传的图片上传到OSS阿里云服务器最后返回一个url路径
        String url = OSSUploadUtil.uploadFile(f, prifix);
        return url;
    }

    /**邮箱验证码*/
    @GetMapping("getEmailInfo")
    Map<String, String> getUserEmailInfo(PayUser user,HttpServletRequest request){
        String email = user.getUseremail();
        try {
            EmailInfo.getEmailInfo(user.getUseremail(),request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String code = (String) request.getSession().getAttribute("Code");
        Map<String,String> map = new HashMap<String,String>();
        map.put("code",code);
        map.put("useremail",user.getUseremail());
        return map;
    }
    /**用户注册*/
    @PostMapping
    Map<String,String> userRegister(PayUser user,@RequestParam("bankNum")String bankNum){
        Map<String,String> map = new HashMap<String,String>();
        try {
            loginService.addUser(user,bankNum);
            map.put("msg","注册成功");
            map.put("flag",LoginConstants.REGISTER_MSG_SUCCESS);
        }catch (Exception e){
            map.put("msg","注册失败");
            map.put("flag",LoginConstants.REGISTER_MSG_ERROR);
        }
        return map;
    }
    /**获得短信验证码*/
    @GetMapping("shortMessageAuthCode")
    public Map<String, String> shortMessageAuthCode(PayUser PayUser, HttpServletRequest request) {
        ShortMsg.shortMsg(PayUser.getUseraccount(),request);
        Map<String,String> map = (Map<String, String>) request.getSession().getAttribute("Info");
        return map;
    }

    /**用户登录*/
    @GetMapping
    public Map<String, String> userLogin(PayUser user, HttpServletRequest request, HttpServletResponse response){

        //根据用户登录账号查询用户信息
        PayUser us = loginService.userLogin(user);
        //声明一个Map用来存放返回的信息
        Map<String,String> map = new HashMap<String,String>();
        //判断us是否为空
        if(us != null){
            //判断登录密码是否与数据库密码一致
            if(Utils.getMd5Pwd(user.getUserpwd()).equals(us.getUserpwd())){
                //将登录后的用户信息放入session中
                request.getSession().getServletContext().setAttribute("us",us);
                //设置返回信息
                map.put("flag", LoginConstants.LOGIN_MSG_SUCCESS);
                map.put("msg","登录成功");
            }else{
                //设置返回信息
                map.put("flag",LoginConstants.LOGIN_MSG_ERROR);
                map.put("msg","密码错误");
            }
        }else{
                //设置返回信息
            map.put("flag",LoginConstants.LOGIN_MSG_NOTFOUND);
            map.put("msg","用户名不存在");
        }
        return map;
    }
}
